package cmd

import (
	"fmt"
	"pkgm/util"
)

type baseCmd struct {
	command     string
	sub_command []string
	docs        string
	run_func    func()
}

var BaseCommand = map[string]baseCmd{}

func init() {
	BaseCommand = map[string]baseCmd{
		"help": {
			command:     "help",
			docs:        "show help",
			sub_command: []string{},
			run_func: func() {
				PrintHelp()
			},
		},
		"version": {
			command:     "version",
			docs:        "show version",
			sub_command: []string{},
			run_func: func() {
				fmt.Println(util.Version)
			},
		},
		"project": {
			command:     "project",
			docs:        "active project env",
			sub_command: []string{"-list", "-search", "-env"},
			run_func: func() {
				fmt.Println("project")
			},
		},
		"env": {
			command:     "env",
			docs:        "Temporary environment for activating packages",
			sub_command: []string{},
			run_func: func() {
				EnvRun()
			},
		},
		"search": {
			command:     "search",
			docs:        "search pkg name",
			sub_command: []string{},
			run_func: func() {
				fmt.Println("search")
			},
		},
	}
}

func RunCommand(command string, sub_command []string) {
	if BaseCommand[command].run_func != nil {
		BaseCommand[command].run_func()
	} else {
		fmt.Println("command not found:", command)
		PrintHelp()
	}
}

func PrintHelp() {
	fmt.Println(util.Description)
	fmt.Println(util.Copyright)
	fmt.Println("Website: ", util.Website)
	fmt.Println("Author: ", util.Author)
	fmt.Println("Date: ", util.Date)
	fmt.Println("Email: ", util.Email)
	fmt.Println("License: ", util.License)
	fmt.Println("Version: ", util.Version)
	fmt.Println("")
	fmt.Println("Usage:")
	for _, v := range BaseCommand {
		fmt.Println("  ", v.command)
		fmt.Println("	", v.docs)
		if len(v.sub_command) > 0 {
			fmt.Println("	 Sub-commands:")
			for _, sub_v := range v.sub_command {
				fmt.Println("	", sub_v)
			}
		}
	}
}
